package engine;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class SchedulerSliderListener implements ChangeListener{
	
	private AlternateStateThreadPoolExecutor processExe;
	private JButton stepButton;
	
	public SchedulerSliderListener (AlternateStateThreadPoolExecutor pe, JButton b) {
		processExe = pe;
		stepButton = b;
		stepButton.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e) { if(stepButton.isEnabled())processExe.nextStep();}});
		
	}
	@Override
	public void stateChanged(ChangeEvent e)  {
		
		 JSlider source = (JSlider)e.getSource();
		    if (!source.getValueIsAdjusting()) {
		        int state = source.getValue();
		        switch(state){
		        	case 1:
		        		processExe.pause();
		        		stepButton.setEnabled(false);
		        		break;
		        	case 2:
		        		processExe.pause();
		        		stepButton.setEnabled(true);
		        		break;
		        	case 3:
		        		processExe.goSlow();
		        		stepButton.setEnabled(false);
		        		break;
		        	case 4:
		        		processExe.goFast();
		        		stepButton.setEnabled(false);
		        		break;
		        }
		    }

	
	}

}